﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

public class HeroDataManager : Singleton<HeroDataManager>
{
    private List<HeroData> heroList;

    public HeroDataManager()
    {
        LoadHeroData();
    }

    private void LoadHeroData()
    {
        if (heroList == null)
            heroList = new List<HeroData>();

        HeroData hero = null;
        TextAsset asset = ResourceMgr.Instance.LoadTextAsset("Tables/herodata");
        JsonData heros = JsonMapper.ToObject(asset.ToString());
        for (int i = 0; i < heros.Count; i++)
        {
            hero = new HeroData();
            hero.id = GameUtilities.ParseIntFromJsonData(heros[i], "id");
            hero.heroName = GameUtilities.ParseStringFromJsonData(heros[i], "name");
            hero.desc = GameUtilities.ParseStringFromJsonData(heros[i], "desc");
            hero.coin = GameUtilities.ParseIntFromJsonData(heros[i], "coin");
            hero.cash = GameUtilities.ParseIntFromJsonData(heros[i], "cash");
            heroList.Add(hero);
        }
    }

    public HeroData GetHeroData(int id)
    {
        if (heroList != null && heroList.Count > 0)
            return heroList.Find(one => one.id == id);
        return null;
    }
}
